package com.qm.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qm.pojo.User;
import com.qm.service.DubboUserService;
import com.qm.util.CookieUtil;
import com.qm.vo.QmResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.JedisCluster;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 @author james
 @create 2021-04-22 8:29
 */
@Controller
@RequestMapping("/user")
public class UserController {

    //消费者启动时，不需要检查是否有服务的提供者
    @Reference(check = false)
    private DubboUserService userService;

    @Autowired
    private JedisCluster jedisCluster;

    /**
     * 完成用户注册
     * 1.url：http://www.qm.com/user/doRegister
     * 2.请求参数：用户名、密码、电话
     * 3.返回值结果：QmResult
     */
    @RequestMapping("/doRegister/{mobileCode}")
    @ResponseBody
    public QmResult doRegister(User user,@PathVariable("mobileCode") String mobileCode) {
        String smsCode = jedisCluster.get(user.getPhone());//从redis中获取短信验证码
        if (mobileCode.equals(smsCode)) {
            userService.saveUser(user);
            return QmResult.success();
        } else {
            return QmResult.fail(203,"验证码错误！");
        }
//        userService.saveUser(user);
//        return QmResult.success();
    }

    /**
     * 业务需求：实现单点登录
     * url：/user/doLogin
     * 参数：{username:_username,password:_password}
     * 返回值：QmResult
     */
    @RequestMapping("/doLogin")
    @ResponseBody
    public QmResult doLogin(User user, HttpServletResponse response) {
        String ticket = userService.doLogin(user);
        if (!StringUtils.hasLength(ticket)) {
            return QmResult.fail();
        }
//        Cookie cookie = new Cookie("QM_TICKET", ticket);
//        cookie.setPath("/");
//        cookie.setMaxAge(7*24*60*60);
//        cookie.setDomain("qm.com");
//        response.addCookie(cookie);
        CookieUtil.addCookie("QM_TICKET",ticket,"qm.com",7*24*60*60,response);
        return QmResult.success();
    }

    /**
     * 用户退出操作，系统重定向首页
     * 1.url：http://www.qm.com/user/logout.html
     * 2.请求参数：无
     * 3.返回值：String 重定向系统首页
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length>0 ) {
            for (Cookie cookie : cookies) {
                if ("QM_TICKET".equalsIgnoreCase(cookie.getName())){
                    String ticket = cookie.getValue();
                    jedisCluster.del(ticket);
//                    cookie.setDomain("qm.com");
//                    cookie.setPath("/");
//                    cookie.setMaxAge(0);
//                    response.addCookie(cookie);
                    CookieUtil.deleteCookie(response, cookie.getName(), "qm.com");
                    break;
                }
            }
        }
        return "redirect:/";
    }

    /**
     * 实现通用的页面跳转(restFul风格)
     */
    @RequestMapping("/{modelName}")
    public String model(@PathVariable String modelName) {
        return modelName;
    }

}
